package com.javasm.controller;

import com.alibaba.fastjson.JSON;
import com.javasm.dao.impl.EditAminDaoImpl;
import com.javasm.entity.AdminUser;
import com.javasm.entity.ReturnCode;
import com.javasm.service.impl.LoginServiceImpl;
import com.javasm.util.MD5Util;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * @author WangJie
 * @version 1.0
 * @Description:
 * @date: 2020/7/18 21:27
 * @since JDK 1.8
 */
@WebServlet("/editAdmin")
public class EditAdminServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        Map<String, Object> returnData = new HashMap<>();
        int result = 0;
        if ("updateUserInfo".equals(method)){
            result = updateUserInfo(req,resp);
        }else if ("deleteUserInfo".equals(method)){
            result = deleteUserInfo(req,resp);
        }else if("insertUserInfo".equals(method)){
            result = insertUserInfo(req,resp);
        }else if ("updateUserAuthority".equals(method)){
            result = updateUserAuthority(req,resp);
        }else if ("alterPassword".equals(method)) {
            result = alterPassword(req, resp);
        }
        // 操作是否成功的判断
        if (result >= 1) {
            returnData.put("returnCode", ReturnCode.SUCCESS.getCode());
            returnData.put("returnMsg", ReturnCode.SUCCESS.getMsg());
        } else {
            returnData.put("returnCode", ReturnCode.ERROR.getCode());
            returnData.put("returnData", ReturnCode.ERROR.getMsg());
        }
        PrintWriter pw = resp.getWriter();
        pw.print(JSON.toJSONString(returnData));
        pw.flush();
        pw.close();
    }

    // 修改密码
    private int alterPassword(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String aid = req.getParameter("aid");
        String newPass = MD5Util.md5(req.getParameter("newPass"));
        return new LoginServiceImpl().alterPassword(aid, newPass);

    }

    // 更新用户权限信息
    private int updateUserAuthority(HttpServletRequest req, HttpServletResponse resp) {
        String aid = req.getParameter("aid");
        String authorityArr = req.getParameter("authorityArr");
        return new EditAminDaoImpl().updateUserAuthority(aid,authorityArr);
    }

    // 新增用户信息
    private int insertUserInfo(HttpServletRequest req, HttpServletResponse resp) {
        AdminUser adminUser = new AdminUser();
        adminUser.setAname(req.getParameter("aname"));
        adminUser.setApwd(MD5Util.md5(req.getParameter("apwd")));
        adminUser.setRole_id(Integer.parseInt(req.getParameter("role")));
        adminUser.setIsvalid(Integer.parseInt(req.getParameter("isvalid")));
        return new EditAminDaoImpl().insertUserInfo(adminUser);
    }

    // 删除用户信息
    private int deleteUserInfo(HttpServletRequest req, HttpServletResponse resp) {
        String aid = req.getParameter("aid");
        return new EditAminDaoImpl().deleteUserInfo(aid);
    }

    // 修改用户信息
    private int updateUserInfo(HttpServletRequest req, HttpServletResponse resp) {
        AdminUser adminUser = new AdminUser();
        adminUser.setAid(Integer.parseInt(req.getParameter("aid")));
        adminUser.setAname(req.getParameter("aname"));
        adminUser.setRole_id(Integer.parseInt(req.getParameter("role")));
        adminUser.setIsvalid(Integer.parseInt(req.getParameter("isvalid")));
        return new EditAminDaoImpl().updateUserInfo(adminUser);
    }

}
